package chan.com.huishang.ui.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView;

import chan.com.huishang.R;
import chan.com.huishang.iviews.IMainViews;
import chan.com.huishang.presenter.impl.MainPresenterImpl;
import chan.com.huishang.ui.fragment.DiscoverFragment;
import chan.com.huishang.ui.fragment.HomeFragment;
import chan.com.huishang.ui.fragment.MineFragment;
import chan.com.huishang.ui.fragment.StudyFragment;
import chan.com.mvplib.factory.CreatePresenter;
import chan.com.mvplib.view.activity.AbstractFragmentActivity;

/**
 * Created by chen on 2017/12/26.
 */
@CreatePresenter(value = MainPresenterImpl.class)
public class MainActivity extends AbstractFragmentActivity<IMainViews, MainPresenterImpl> implements IMainViews, View.OnClickListener {

    private TextView tv_home;
    private TextView tv_study;
    private TextView tv_mine;
    private TextView tv_discover;

    private HomeFragment mHomefragment;
    private DiscoverFragment mDiscoverFragment;
    private MineFragment mMineFragment;
    private StudyFragment mStudyfragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_home = (TextView) findViewById(R.id.tv_home);
        tv_home.setSelected(true);
        tv_study = (TextView) findViewById(R.id.tv_study);
        tv_mine = (TextView) findViewById(R.id.tv_mine);
        tv_discover = (TextView) findViewById(R.id.tv_discover);
        mHomefragment = new HomeFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.main_container, mHomefragment).commit();
        tv_home.setOnClickListener(this);
        tv_study.setOnClickListener(this);
        tv_mine.setOnClickListener(this);
        tv_discover.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        changeSelect(v.getId());
        changeFragment(v.getId());
    }

    private void changeFragment(int resId) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();//开启一个Fragment事务

        hideFragments(transaction);//隐藏所有fragment

        if (resId == R.id.tv_home) {
            if (mHomefragment == null) {//如果为空先添加进来.不为空直接显示
                mHomefragment = new HomeFragment();
                transaction.add(R.id.main_container, mHomefragment);
            } else {
                transaction.show(mHomefragment);
            }
        } else if (resId == R.id.tv_study) {
            if (mStudyfragment == null) {
                mStudyfragment = new StudyFragment();
                transaction.add(R.id.main_container, mStudyfragment);
            } else {
                transaction.show(mStudyfragment);
            }
        } else if (resId == R.id.tv_discover) {
            if (mDiscoverFragment == null) {
                mDiscoverFragment = new DiscoverFragment();
                transaction.add(R.id.main_container, mDiscoverFragment);
            } else {
                transaction.show(mDiscoverFragment);
            }
        } else if (resId == R.id.tv_mine) {
            if (mMineFragment == null) {
                mMineFragment = new MineFragment();
                transaction.add(R.id.main_container, mMineFragment);
            } else {
                transaction.show(mMineFragment);
            }
        }
        transaction.commit();//一定要记得提交事务
    }

    private void hideFragments(FragmentTransaction transaction) {
        if (mMineFragment != null)//不为空才隐藏,如果不判断第一次会有空指针异常
            transaction.hide(mMineFragment);
        if (mDiscoverFragment != null)
            transaction.hide(mDiscoverFragment);
        if (mStudyfragment != null)
            transaction.hide(mStudyfragment);
        if (mHomefragment != null)
            transaction.hide(mHomefragment);
    }

    private void changeSelect(int id) {
        tv_home.setSelected(false);
        tv_discover.setSelected(false);
        tv_mine.setSelected(false);
        tv_study.setSelected(false);
        switch (id) {
            case R.id.tv_discover:
                tv_discover.setSelected(true);
                break;
            case R.id.tv_home:
                tv_home.setSelected(true);
                break;
            case R.id.tv_study:
                tv_study.setSelected(true);
                break;
            case R.id.tv_mine:
                tv_mine.setSelected(true);
                break;
        }
    }
}
